package com.youlai.boot.modules.area.controller;

import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import com.youlai.boot.modules.area.model.AreaProductForm;
import com.youlai.boot.modules.area.model.AreaProductVO;
import com.youlai.boot.modules.area.service.AreaProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Tag(name = "17.1 区域商品接口")
@RestController
@RequestMapping("/api/v1/area-product")
@RequiredArgsConstructor
public class AreaProductController {

    private final AreaProductService areaProductService;

    @Operation(summary = "区域商品分页列表")
    @GetMapping("/page")
    public PageResult<AreaProductVO> listAreaProducts(
            @RequestParam(required = false) Long areaId,
            @RequestParam(required = false) Integer showFlag,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<AreaProductVO> result = areaProductService.getAreaProductPage(areaId, showFlag, pageNum, pageSize);
        return PageResult.success(result);
    }

    @Operation(summary = "区域商品详情")
    @GetMapping("/{id}")
    public Result<AreaProductVO> getAreaProduct(@PathVariable Long id) {
        AreaProductVO areaProduct = areaProductService.getAreaProductById(id);
        return Result.success(areaProduct);
    }

    @Operation(summary = "添加区域商品")
    @PostMapping
    public Result<Long> addAreaProduct(@Valid @RequestBody AreaProductForm areaProduct) {
        Long id = areaProductService.saveAreaProduct(areaProduct);
        return Result.success(id);
    }

    @Operation(summary = "修改区域商品")
    @PutMapping("/{id}")
    public Result<Boolean> updateAreaProduct(
            @PathVariable Long id,
            @Valid @RequestBody AreaProductForm areaProduct) {
        areaProduct.setId(id);
        areaProductService.updateAreaProduct(areaProduct);
        return Result.success(true);
    }

    @Operation(summary = "删除区域商品")
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteAreaProduct(@PathVariable Long id) {
        boolean result = areaProductService.deleteAreaProduct(id);
        return Result.judge(result);
    }
}